package com.jianbing.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

/**
 * Created by Han,Tixiang on 2016/8/16 0016.
 */
public class FileUtils {
    private static final Log log = LogFactory.getLog(FileUtils.class);
   /* public static void writeFile(String filePath, String fileName, String content, boolean notCreateNewFile) {
        try {

            File file = new File(filePath + fileName);

            if (!notCreateNewFile) {
                file.delete();
                file.createNewFile();
            }

            if (notCreateNewFile && !file.exists()) {
                file.createNewFile();

            }

            //    FileWriter fw = new FileWriter(file.getName(),true);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true), "UTF-8"));
            bw.write(content);
            bw.close();


        } catch (IOException e) {

        }
    }
*/

    public static void createDir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    public static String readByNIO(String file) {
        //第一步 获取通道
        FileInputStream fis = null;
        FileChannel channel = null;
        try {
            File f = new File(file);
            if (!f.exists()) {
                f.createNewFile();
            }
            fis = new FileInputStream(file);
            channel = fis.getChannel();
            //文件内容的大小
            int size = (int) channel.size();

            //第二步 指定缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(size);
            //第三步 将通道中的数据读取到缓冲区中
            channel.read(buffer);

            Buffer bf = buffer.flip();


            byte[] bt = buffer.array();
            String result = new String(bt, 0, size, "UTF-8");

            buffer.clear();
            buffer = null;
            return result;
        } catch (Exception e) {
            log.error(e.toString(),e);
        } finally {
            try {
                channel.close();
                fis.close();
            } catch (IOException e) {
                log.error(e.toString(),e);
            }
        }
        return null;
    }


    public static String readChannel(FileChannel channel) throws Exception{
        //文件内容的大小
        int size = (int) channel.size();

        //第二步 指定缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(size);
        //第三步 将通道中的数据读取到缓冲区中
        channel.read(buffer);

        Buffer bf = buffer.flip();


        byte[] bt = buffer.array();
        String result = new String(bt, 0, size, "UTF-8");

        buffer.clear();
        buffer = null;
        return result;
    }

    public static void writeFileByNIO(String filePath, String fileName, String content,boolean notCreateNewFile) throws IOException {
        FileOutputStream fos = null;
        FileChannel fc = null;
        ByteBuffer buffer = null;
        try {
            log.info(filePath);
            File file = new File(filePath + fileName);

            if (!notCreateNewFile) {
                file.delete();
                file.createNewFile();
            }

            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            //第一步 获取一个通道
            fc = fos.getChannel();
            //buffer=ByteBuffer.allocate(1024);
            //第二步 定义缓冲区
            buffer = ByteBuffer.wrap(content.getBytes(Charset.forName("UTF-8")));
            //将内容写到缓冲区
            fos.flush();
            fc.write(buffer);

        }  finally {
            try {
                fc.close();
                fos.close();
            } catch (IOException e) {
                log.error(e.toString(),e);
            }

        }
    }

    public static void main(String[] args) throws IOException {
        String logPath = "./logs/";
        String report = "test.txt";
        writeFileByNIO(logPath , report + ".2016-08-18", "test",false);
    }
}